/*
 * @(#)UserActionImpl.java	2013-3-31 下午8:07:52
 * intranet
 * Copyright 2013 Thuisoft, Inc. All rights reserved.
 * THUNISOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.thunisoft.intranet.user.action.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.thunisoft.intranet.domain.TUser;
import com.thunisoft.intranet.user.action.IUserAction;
import com.thunisoft.intranet.user.service.IUserService;

/**
 * UserActionImpl
 * 
 * @author ice
 * @version 1.0
 * 
 */
public class UserActionImpl extends ActionSupport implements IUserAction {

    /**
     * 序列号
     */
    private static final long serialVersionUID = 2687309354174130071L;

    /**
     * 验证码
     */
    private String identify;

    /**
     * 用户对象
     */
    private TUser user;

    /**
     * 用户Service对象
     */
    @Autowired
    private IUserService userService;

    /**
     * @return the identify
     */
    public String getIdentify() {
        return identify;
    }

    /**
     * @param identify
     *            the identify to set
     */
    public void setIdentify(String identify) {
        this.identify = identify;
    }

    /**
     * @return the user
     */
    public TUser getUser() {
        return user;
    }

    /**
     * @param user
     *            the user to set
     */
    public void setUser(TUser user) {
        this.user = user;
    }

    @Override
    public String login() {
        ActionContext context = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) context
                .get(ServletActionContext.HTTP_REQUEST);
        String randomString = (String) request.getSession().getAttribute(
                "randomString");

        if (!identify.equals(randomString)) {
            return null;
        }
        return "success";
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.thunisoft.intranet.user.action.IUserAction#register()
     */
    @Override
    public String register() {
        userService.saveOrUpdate(user);
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.thunisoft.intranet.user.action.IUserAction#toRegister()
     */
    @Override
    public String toRegister() {
        return "toRegister";
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.thunisoft.intranet.user.action.IUserAction#query()
     */
    @Override
    public String query() {
        List<TUser> list = userService.getAllUserList();
        for (TUser t : list) {
            System.out.println(t.getCUsername());
        }
        return null;
    }

}
